package dao;

import com.alibaba.fastjson.JSON;
import domin.Book;
import domin.BookCategory;
import domin.BookStatus;
import lombok.extern.slf4j.Slf4j;
import util.CommonDao;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

@Slf4j
public class BookStautsDao {
    private static BookStautsDao bookStauts = new BookStautsDao();
    private BookStautsDao(){}
    public static BookStautsDao getInstance(){
        return  bookStauts;
    }
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;
    public BookStatus find (Connection connection , int id) throws SQLException {
        //预编译语句
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM bookstatus where id=?");
        preparedStatement.setInt(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        BookStatus  bookStatus = null;
        if (resultSet.next()){
            bookStatus= new BookStatus();
            bookStatus.setId(resultSet.getInt("id")); ;
            bookStatus.setStatus(resultSet.getString("Status"));
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return bookStatus;
    }
    public Collection<BookStatus> findAll(Connection connection, Collection<Condition> condition, Pagination pagination) throws SQLException {
        Collection<BookStatus> books = new TreeSet<BookStatus>();
        int totalNum = CommonDao.getInstance().count(connection,"bookstatus");
        String sql_select_all = "SELECT * FROM bookstatus ";
        if (condition != null){
            sql_select_all += Condition.toWhereClause(JSON.toJSONString(condition));
            log.trace(sql_select_all);
        }
        if (pagination != null){
            sql_select_all += pagination.toLimitClause(totalNum);
            log.trace(sql_select_all);
        }
        this.preparedStatement = connection.prepareStatement(sql_select_all);
        this.resultSet = preparedStatement.executeQuery();
        while (this.resultSet.next()){
            BookStatus bookStatus = new BookStatus();
            bookStatus= new BookStatus();
            bookStatus.setId(resultSet.getInt("id")); ;
            bookStatus.setStatus(resultSet.getString("Status"));
            books.add(bookStatus);
        }
        JdbcHelper.close(this.resultSet,this.preparedStatement);
        return books;
    }
    public boolean update(Connection connection, BookStatus book) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement("update bookstatus set Status=? " +
                "where id=?");
        preparedStatement.setString(1,book.getStatus());
        preparedStatement.setInt(2,book.getId());
        int rowAffected = preparedStatement.executeUpdate();
        JdbcHelper.close(null,preparedStatement);
        return rowAffected > 0;
    }
}
